<?php

class CanteenController extends Controller
{
    /**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
    
    public function actionIndex($table_id = 1)
    {
        $this->current_page = 'canteen';
        
        $totalTables = Yii::app()->settings->get('system', 'table');
        
        $listDrinks = Drinks::model()->findAll();

        $listClothings = ClothingType::model()->with('clothings')->findAll();
        
        $arrStadiums = Stadiums::model()->findAllByAttributes(array('visible' => 1));
        
        $this->render('index',array(
            'table_id' => $table_id,
            'totalTables' => $totalTables,
            'listDrinks' => $listDrinks,
            'listClothings' => $listClothings,
            'arrStadiums' => $arrStadiums,
        ));
    }
    
    public function actionDetail($table_id)
    {
        //CVarDumper::dump($_POST, 10, false);exit;
        if (isset($_POST['Drinks']))
        {
            foreach ($_POST['Drinks'] as $drink_id => $number)
                //if ($number > 0)
                {
                    $model = OrdersTables::model()->findByAttributes(array(
                        'table_id' => $table_id,
                        'drink_id' => $drink_id,
                    ));
                    if ($model) {
                        $model->number = $number;
                        if ($number==0) 
                            $model->delete();
                        else 
                            $model->save();
                    } else {
                        $model = new OrdersTables;
                        $model->table_id = $table_id;
                        $model->drink_id = $drink_id;
                        $model->number = $number;
                        $model->save();
                        
                        //$drink = Drinks::model()->findAllByPk($drink_id)->change($number);
                    }
                }
            $checkout = OrdersTables::model()->checkoutByTable($table_id); 
            echo Yii::app()->numberFormatter->format('###,###,###,###', $checkout['total_money']);
            Yii::app()->end();
        }
        
        if (isset($_POST['Clothings']))
        {
            foreach ($_POST['Clothings'] as $clothing_id => $number)
                //if ($number > 0)
                {
                    $model = OrdersTables::model()->findByAttributes(array(
                        'table_id' => $table_id,
                        'clothing_id' => $clothing_id,
                    ));
                    if ($model) {
                        $model->number = $number;
                        if ($number==0) $model->delete();
                        else $model->save();
                    } else {
                        $model = new OrdersTables;
                        $model->table_id = $table_id;
                        $model->clothing_id = $clothing_id;
                        $model->number = $number;
                        $model->save();
                    }
                }
            $checkout = OrdersTables::model()->checkoutByTable($table_id); 
            echo Yii::app()->numberFormatter->format('###,###,###,###', $checkout['total_money']);
            Yii::app()->end();
        }
        
        $stadium = null;
        $checkouts = null;
        if ($table_id) {
            $stadium = StadiumsTables::model()->with('stadiums')->findAllByAttributes(array(
                'table_name' => $table_id
            ));
            $checkouts = OrdersTables::model()->checkoutByTable($table_id);
        }
        
        $this->renderPartial('detail',array(
            'table_id' => $table_id,
            'stadium' => $stadium,
            'checkouts' => $checkouts,
        ));
    }
    
    public function actionMapStadium($table_id)
    {
        if ($_POST['MapStadium']) {
            $stadium = $_POST['MapStadium'];
            $stadiumstable = StadiumsTables::model()->countByAttributes(array(
                'table_name' => $table_id,
                'stadium_id' => $stadium,
            ));
            if (!$stadiumstable) {
                $stadiumstable = new StadiumsTables;
                $stadiumstable->stadium_id = $stadium;
                $stadiumstable->table_name = $table_id;
                $stadiumstable->save();
            }
        }
    }
}